package cn.wolfcode.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.enums.AppointmentStarusEnum;
import cn.wolfcode.enums.ConsumptionEnums;
import cn.wolfcode.mapper.ConsumptionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ConsumptionServiceImpl implements IConsumptionService {

    @Autowired
    private ConsumptionMapper consumptionMapper;

    @Autowired
    private IAppointmentService appointmentService;

    @Override
    public void save(Consumption consumption) {
        consumptionMapper.insert(consumption);
    }

    @Override
    public void delete(Long id) {
        consumptionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Consumption consumption) {
        consumptionMapper.updateByPrimaryKey(consumption);
    }

    @Override
    public Consumption get(Long id) {
        return consumptionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Consumption> listAll() {
        return consumptionMapper.selectAll();
    }

    @Override
    public PageInfo<Consumption> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Consumption> consumptions = consumptionMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Consumption>(consumptions);
    }

    @Override
    public Long saveAppoiment(Long appointmentId) {
//        修改从预约单页面跳转过去的客户的状态
        appointmentService.updateStatus(appointmentId, AppointmentStarusEnum.CONSUME.getValue());
//        通过id查询用户的信息
        Appointment appointment = appointmentService.get(appointmentId);
//        创建新的结算单对象,插进去
        Consumption consumption=new Consumption();
        Date now = new Date();
//        消费单号
        consumption.setCno(DateUtil.format(now,"yyyyMMddHHmmss")+ RandomUtil.randomNumbers(5));
//        消费状态
        consumption.setStatus(ConsumptionEnums.CONSUME.getValue());
        System.out.println("ssssss"+consumption.getStatus());
//        客户名字
        consumption.setCustomerName(appointment.getContactName());
        //        客户电话
        consumption.setCustomerTel(appointment.getContactTel());
       //预约单号
        consumption.setAppointmentAno(appointment.getAno());
//        预约门店
        consumption.setBusinessId(appointment.getBusiness());

        consumptionMapper.insert(consumption);

        return consumption.getId();
    }

    @Override
    public void payFianl(Long id) {
        Consumption c=consumptionMapper.selectByPrimaryKey(id);
        c.setStatus(ConsumptionEnums.AUDIT.getValue());
        c.setPayeeId(UserContext.getUser());
        Date now = new Date();
        c.setPayTime(now);
        consumptionMapper.updateFinalPay(c);
    }

//    审核
    @Override
    public void auditById(Long id, Integer status) {
        Consumption c=consumptionMapper.selectByPrimaryKey(id);
        c.setStatus(status);
        c.setAuditorId(UserContext.getUser());
        Date now = new Date();
        c.setAuditTime(now);
        consumptionMapper.auditById(c);
    }
}
